<template>
  <router-view></router-view>
  <div class="employeesMng-box" v-if="isIndex">
    <!-- <div class="tips-box">
      <h1>岗位管理</h1>
      <span>可以根据您的需要，自定义岗位权限，并将该岗位的权限赋予给员工。</span>
    </div> -->
    <div class="content-box w-card">
      <div class="title-box">
        <span>线下代充</span>
        <el-button style="margin-left:auto;" type="primary" icon="Plus" @click="addOrder">新增代充记录</el-button>
      </div>
      <div class="search-box">
        <div class="form-item">
          <span>订单编号：</span>
          <el-input v-model="searchForm.orderId" clearable placeholder="请输入订单编号" @keypress.enter="searchHandle" />
        </div>
        <div class="form-item">
          <span>代充店铺：</span>
          <el-select style="width:100%" v-model="searchForm.toShopId">
            <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
          </el-select>
        </div>
        <div class="form-item" style="width:30%;max-width:480px;">
          <span>操作时间：</span>
          <el-date-picker style="width:100%" v-model="searchForm.date" type="datetimerange" range-separator="至"
            start-placeholder="开始时间" end-placeholder="结束时间" />
        </div>
        <el-button type="primary" @click="searchHandle">查询</el-button>
        <el-button style="margin-left:0;" @click="resetHandle">重置</el-button>
      </div>
      <div class="table-box">
        <el-table :height="tableHeight" :header-cell-style="{background:'#d6dee5',color:'#000000',fontSize:'14px'}"
          :cell-style="{fontSize:'14px',color:'#7b7b7d'}" :data="tableData" stripe>
          <el-table-column type="index" width="55" label="序号" align="center" />
          <el-table-column prop="orderId" label="订单编号" align="center">
            <template v-slot="scope">
              <el-link type="primary" @click="lookDetail(scope.row)">{{ scope.row.orderId || '' }}</el-link>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="tradeNo" label="交易流水" align="center">
            <template v-slot="scope">
              {{ scope.row.tradeNo || '' }}
            </template>
          </el-table-column> -->
          <el-table-column prop="toShopIdName" label="代充商户" align="center">
            <template v-slot="scope">
              {{ scope.row.toShopIdName || '' }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="payType" label="支付方式" align="center">
            <template v-slot="scope">
              {{ scope.row.payType || '' }}
            </template>
          </el-table-column> -->
          <el-table-column prop="fee" label="支付金额(元)" align="center">
            <template v-slot="scope">
              {{ numFormat(scope.row.fee/100) || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="remark" label="备注" align="center">
            <template v-slot="scope">
              {{ scope.row.remark || '' }}
            </template>
          </el-table-column>
          <!-- <el-table-column prop="chargeTime" label="交易时间" align="center" width="180">
            <template v-slot="scope">
              {{ scope.row.chargeTime ? getDateTime(scope.row.chargeTime)[6] : '' }}
            </template>
          </el-table-column> -->
          <el-table-column prop="userName" label="操作人" align="center">
            <template v-slot="scope">
              {{ scope.row.userName || '' }}
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="操作时间" align="center" width="180">
            <template v-slot="scope">
              {{ scope.row.createTime ? getDateTime(scope.row.createTime)[6] : '' }}
            </template>
          </el-table-column>
        </el-table>
        <div class="table-footer">
          <el-pagination v-model:currentPage="page" v-model:page-size="size" :page-sizes="[10, 20, 50, 100]" small
            :background="true" layout="total, sizes, prev, pager, next, jumper" :total="total" @size-change="handleSizeChange"
            @current-change="handleCurrentChange" />
        </div>
      </div>
    </div>
  </div>
  <edit-dialog ref="editDialogRef" @dialogClose="editDialogClose" />
</template>

<script>
import { onMounted, reactive, ref, toRefs, watch } from 'vue'
import { getWindowWidthAndHeight, warningDeal, getDateTime, successDeal, numFormat } from '@/utils/utils'
import api from './api'
import editDialog from './components/editDialog.vue'
import { useRoute, useRouter } from 'vue-router'
export default {
  components: {
    editDialog,
  },
  setup() {
    const route = useRoute()
    const router = useRouter()

    const editDialogRef = ref(null)

    const state = reactive({
      isIndex: true,
      dealData: null,
      dealType: null,
      page: 1,
      size: 10,
      total: 0,
      tableHeight: 0,
      searchForm: {
        toShopId: null,
      },
      tableData: [],
      shopOptions: [],
    })

    watch(() => route.matched, (newVal, oldVal) => {
      setIsIndex()
    })

    function setIsIndex() {
      if (route.matched[route.matched.length - 1].path == '/offlineReplacement') {
        state.isIndex = true
        getTableData()
      } else {
        state.isIndex = false
      }
    }

    function getTableData() {
      const obj = {
        page: state.page,
        size: state.size,
        ...state.searchForm,
        startTime: (state.searchForm.date && state.searchForm.date[0]) ? state.searchForm.date[0].getTime() : null,
        endTime: (state.searchForm.date && state.searchForm.date[1]) ? state.searchForm.date[1].getTime() : null,
      }
      api.accountChargeFlow(obj).then(res => {
        if (res.code == '200') {
          state.tableData = res.data.list
          state.total = res.data.total
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function searchHandle() {
      //搜索
      state.page = 1
      getTableData()
    }

    function resetHandle() {
      state.searchForm = {
        toShopId: null
      }
      searchHandle()
    }

    //密码校验通过
    function verification() {
      if (state.dealType == 1) {
      } else if (state.dealType == 2) {
        userStateChange(1)
      } else if (state.dealType == 3) {
        userStateChange(0)
      } else if (state.dealType == 4) {
        userDelete()
      }
    }

    function userStateChange(is) {
      const obj = {
        state: is,
        roleIds: [state.dealData.roleId],
      }
      api.batchState(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function userDelete() {
      const obj = {
        roleIds: [state.dealData.roleId],
      }
      api.roleDelRole(obj).then(res => {
        if (res.code == '200') {
          successDeal(res.msg)
          getTableData()
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function addOrder() {
      editDialogRef.value.dialogShow()
    }

    function editDialogClose(is) {
      if (is) {
        getTableData()
      }
    }

    function getShop() {
      api.chooseShop().then(res => {
        if (res.code == '200') {
          let myArr = []
          myArr = [{ shopId: null, shopName: '全部店铺' }, ...res.data.list]
          state.shopOptions = myArr
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function lookDetail(data) {
      router.push({ path: '/offlineReplacement/detail', query: { data: JSON.stringify(data) } })
    }

    function handleSizeChange(e) {
      state.size = e
      getTableData()
    }

    function handleCurrentChange(e) {
      state.page = e
      getTableData()
    }

    onMounted(() => {
      let winHeight = getWindowWidthAndHeight()
      state.tableHeight = winHeight.windowHeight - 290
      setIsIndex()
      getShop()
    })

    return {
      ...toRefs(state),
      editDialogRef,
      searchHandle,
      resetHandle,
      getDateTime,
      verification,
      editDialogClose,
      addOrder,
      numFormat,
      lookDetail,
      handleSizeChange,
      handleCurrentChange,
    }
  }
}
</script>

<style lang="scss" scoped>
.employeesMng-box {
  width: 100%;
  height: 100%;
  display: flex;
  flex-flow: column;
  .content-box {
    flex-grow: 1;
    box-sizing: border-box;
    padding: 20px;
    .title-box {
      display: flex;
      align-items: center;
      padding-bottom: 20px;
      > span {
        font-size: 16px;
        font-weight: bold;
      }
    }
    .search-box {
      display: flex;
      align-items: center;
      gap: 20px;

      .form-item {
        width: 25%;
        max-width: 300px;
        display: flex;
        align-items: center;
        gap: 5px;

        > span {
          font-size: 14px;
          flex-shrink: 0;
        }
      }
    }
    .table-box {
      margin-top: 20px;
      .table-footer {
        margin-top: 20px;
        display: flex;
        justify-content: flex-end;
      }
    }
  }
}
.state0 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #ff3939;
}
.state1 {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background-color: #03b615;
}
.table-handle-box {
  display: flex;
  gap: 5px;
  align-items: center;
  justify-content: center;
}
</style>